<?php
/**
 * Created by PhpStorm.
 * User: Raytine
 * Date: 2018/8/29
 * Time: 15:08
 */
require_once dirname(__DIR__)."/base.php";

$orderData = new \PddWechat\Models\Pddorder\Data\PddOrderData();
$userData = new \PddWechat\Models\User\Data\UserData();
$userMoneyRecordData = new \PddWechat\Models\User\Data\UserMoneyRecordData();
$redis_key = "calculatemoney";
$di = \Phalcon\DI::getDefault();

if(\Dai\Framework\Library\RedisExt::getInstance()->exists($redis_key)) {

    $listLen = \Dai\Framework\Library\RedisExt::getInstance()->lLen($redis_key);
    for ($i = 0; $i < $listLen; $i++) {
        $di->getShared('db')->query("BEGIN");
        $orderSn = \Dai\Framework\Library\RedisExt::getInstance()->rPop($redis_key);
        echo $orderSn . "\n";
        //查询订单是否存在
        $orderInfo = $orderData->selectByOrderId($orderSn);
        if(empty($orderInfo)) {
            echo $orderSn . " is not exists\n";
            continue;
        }
        if(!empty($orderInfo->uid) && !empty($orderInfo->uidMoney)) {
            $res = $userData->updateUserMoneyById($orderInfo->uid,floatval($orderInfo->uidMoney/100));
            if(!$res) {
                $di->getShared('db')->query("ROLLBACK");
            }
            $userMoneyRecordDo = new \PddWechat\Models\User\DataObject\UserMoneyRecordDo();
            $userMoneyRecordDo->uid = $orderInfo->uid;
            $userMoneyRecordDo->money = $orderInfo->uidMoney;
            $userMoneyRecordDo->type = 1;
            $userMoneyRecordData->insert($userMoneyRecordDo);
        }
        if(!empty($orderInfo->inviteUid) && !empty($orderInfo->inviteUidMoney)) {
            $res = $userData->updateUserMoneyById($orderInfo->inviteUid,floatval($orderInfo->inviteUidMoney/100));
            if(!$res) {
                $di->getShared('db')->query("ROLLBACK");
            }
            $userMoneyRecordDo = new \PddWechat\Models\User\DataObject\UserMoneyRecordDo();
            $userMoneyRecordDo->uid = $orderInfo->inviteUid;
            $userMoneyRecordDo->money = $orderInfo->inviteUidMoney;
            $userMoneyRecordDo->type = 1;
            $userMoneyRecordData->insert($userMoneyRecordDo);
        }
        if(!empty($orderInfo->inviteParentUid) && !empty($orderInfo->inviteParentUidMoney)) {
            $res = $userData->updateUserMoneyById($orderInfo->inviteParentUid,floatval($orderInfo->inviteParentUidMoney/100));
            if(!$res) {
                $di->getShared('db')->query("ROLLBACK");
            }
            $userMoneyRecordDo = new \PddWechat\Models\User\DataObject\UserMoneyRecordDo();
            $userMoneyRecordDo->uid = $orderInfo->inviteParentUid;
            $userMoneyRecordDo->money = $orderInfo->inviteParentUidMoney;
            $userMoneyRecordDo->type = 1;
            $userMoneyRecordData->insert($userMoneyRecordDo);
        }
        //更新已经结算过的订单flag
        $res = $orderData->updateFlagOrderId($orderInfo->id);
        if(!$res) {
            $di->getShared('db')->query("ROLLBACK");
        }
        $di->getShared('db')->query("COMMIT");
    }
} else {
    echo "redis list is empty\n";
}